package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.AdminRoleService;
import com.cskaoyan.market.service.impl.AdminRoleServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 管理员角色
 * @author: 刘国兴
 **/
@WebServlet("/admin/role/*")
public class AdminRoleController extends HttpServlet {

    AdminRoleService service = new AdminRoleServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        disPatchRequest(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        disPatchRequest(req, resp);
    }

    /**
     * @author 刘国兴
     * @description 请求分发
     */
    private void disPatchRequest(HttpServletRequest req, HttpServletResponse resp) {
        String pathInfo = req.getPathInfo();
        try {
            switch (pathInfo) {
                case "/options":
                    options(req, resp);
                    break;
                default:
                    resp.sendRedirect(req.getRequestURI());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author 刘国兴
     * @description 获取管理员角色列表
     */
    private void options(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String options = service.options();
        resp.getWriter().println(options);
    }
}
